package org.spring.aop.framework.adapter;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.spring.aop.AfterAdvice;
import org.spring.aop.AfterReturningAdvice;
import org.tool.core.lang.Assert;

import java.io.Serializable;

/**
 * 封装一个{@link AfterReturningAdvice}的拦截器。由AOP框架内部使用;应用程序开发人员不应该直接使用这个类。
 *
 * @author 李坤
 * @date 2022/4/19 16:27
 */
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable {

    /**
     * 方法后置通知
     */
    private final AfterReturningAdvice returningAdvice;

    public AfterReturningAdviceInterceptor(AfterReturningAdvice returningAdvice) {
        Assert.notNull(returningAdvice, "returningAdvice must not be null");

        this.returningAdvice = returningAdvice;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // 执行原方法逻辑
        Object proceed = invocation.proceed();
        // 执行后置通知
        returningAdvice.afterReturning(invocation.getThis(), proceed, invocation.getMethod(), invocation.getArguments());

        return proceed;
    }

}
